<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>管理页面</title>
    <script src="/static/js/jquery-3.2.1.min.js"></script>
    <!-- Site CSS -->
    <link href="/static/bootstrap-3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="/static/bootstrap-3.3.7/js/bootstrap.min.js"
            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script>
</head>
<body>
<!--模态框-->
<div class="modal fade" id="category_Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">栏目修改</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="postForm">
                    <!--下拉框-->
                    <input type="hidden" id="id" name="category_id" value="">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">所属栏目:</label>
                        <div class="col-sm-3">
                        <select class="form-control" name="pid" id="pid">
                            <option value="0">顶级栏目</option>
                            {$category}
                        </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">栏目名字:</label>
                        <div class="col-sm-10">
                            <input type="text" name="category_name" class="form-control" id="category_name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">栏目简介:</label>
                        <div class="col-sm-10">
                            <textarea type="text" name="category_text" id="category_text" class="form-control"></textarea>
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="create">确认</button>
            </div>
        </div>
    </div>
</div>

<!-- 中间页面顶部导航 -->
<ol class="breadcrumb">
    <li class="active"><a href="{:url('backend/Index/index')}">首页</a></li>
    <li class="active">栏目管理</li>
    <li class="active">栏目列表</li>
</ol>

<div class="panel panel-default">
    <div class="panel-heading">
        搜索
    </div>
    <div class="panel-body">
        <form id="searchForm" method="post" class="form-inline" action="{:url('backend/Category/index')}">
            <div class="form-group">
                <label for="search_name">名称</label>
                <input type="text" class="form-control" name="search_name" value="{$map.search_name ? $map.search_name : ''}" id="search_name" placeholder="请输入栏目名称">
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-default">开始搜索</button>
            </div>
        </form>
    </div>
</div>
<!--
    列表展示
-->
<button type="button" class="btn btn-sm btn-primary" id="btn-model" data-target="#category_Modal" data-toggle="modal" data-whatever="create">新增</button>

<div class="table-responsive">

    <table class="table table-striped ">
        <thead>
        <tr>
            <td>id</td>
            <td>所属栏目</td>
            <td>栏目名</td>
            <td>栏目简介</td>
            <td>栏目创建时间</td>
            <td>栏目最后修改时间</td>
            <td>操作</td>
        </tr>
        </thead>
        <tbody id="lists">

        {volist name="categorys" id="vo"}
        <tr id="list-{$vo.category_id}">
            <td>{$vo.category_id}</td>
            <td>{$vo.pid|getCategory}</td>
            <td>{$vo.category_name}</td>
            <td>{$vo.category_text}</td>
            <td>{$vo.create_time}</td>
            <td>{$vo.update_time}</td>
            <td>
                <input type="hidden" id="">
                <button type="button" class="btn btn-sm btn-danger" data-id="{$vo.category_id}" data-name="{$vo.category_name}" data-type="del">删除</button>
                <button type="button" class="btn btn-sm btn-primary" data-id="{$vo.category_id}" data-type="update" data-target="#category_Modal" data-toggle="modal" data-whatever="update">修改</button>
            </td>
        </tr>
        {/volist}
        </tbody>
    </table>
</div>
<div class="pagenation">
    {$page}
</div>
<script type="text/javascript">
    //删除按钮
    $("#lists button").click(function () {
        var category_name = $(this).attr('data-name');
        var category_id = $(this).attr('data-id');
        var type = $(this).attr('data-type');
        if (type === 'del')
        {
        if(confirm("确定删除栏目名为"+category_name+"吗？")){
            $.ajax({
                url: "{:url('backend/category/delete')}",
                method: "POST",
                data: {"category_id": category_id},
                dataType: "json",
                success: function (res) {
                    if (res.status === 0) {
                        alert(res.msg);
                        return false;
                    }
                    $("#list-" + category_id).remove();
                }
            });
        }
        }
        else if (type === 'update')
        {
            $.ajax({
                url: "{:url('backend/category/read')}",
                method: "POST",
                data: {"category_id": category_id},
                dataType: "json",
                success: function (res) {
                    if (res.status === 0) {
                        alert(res.msg);
                        return false;
                    }
                    var categoryData = res.data;
                    
                    var tree = '<option value="0">顶级栏目</option>'+categoryData.tree;
                    $("#pid").empty();
                    $("#pid").html(tree);

                    $("#category_name").val(categoryData.category_name);
                    $("#category_text").val(categoryData.category_text);
                    $("#id").val(categoryData.category_id);
                }
            });
        }
    });

    //确认新增按钮
    $("#create").click(function () {
        var type = $(this).attr('data-type');
        //不同的模态框的报错按钮
        if (type === 'update'){
            $.ajax({
                url: "{:url('backend/Category/update')}",
                method: "POST",
                data: $("#postForm").serialize(),
                dataType: "json",
                success: function (res) {
                    if (res.status === 0) {
                        alert(res.msg);
                        return false;
                    }
                    $('#category_Modal').modal('hide');
                    window.location.reload();
                }
            });
        }else if (type === 'create'){
            $.ajax({
                url: "{:url('backend/Category/create')}",
                method: "POST",
                data: $("#postForm").serialize(),
                dataType: "json",
                success: function (res) {
                    if (res.status === 0) {
                        alert(res.msg);
                        return false;
                    }
                    $('#category_Modal').modal('hide');
                    window.location.reload();
                }
            });
        }
    });

    $('#category_Modal').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        var recipient = button.data('whatever');
        var modal = $(this);
        if (recipient === 'create')
        {
            $("#id").remove();
            $("<option></option>").append(this.pid).attr("value",0);
            $("#category_name").val("");
            $("#category_text").val("");
            modal.find('.modal-title').text('新增栏目');
            modal.find('#create').attr('data-type', recipient);
        }
        else if (recipient === 'update')
        {
            modal.find('.modal-title').text('修改栏目');
            modal.find('#create').attr('data-type', recipient);
        }
    });
</script>
</body>
</html>